package com.qiangqiang.task.server.service;

import com.qiangqiang.task.server.dto.FollowDTO;
import com.qiangqiang.task.server.dto.MomentsDTO;
import com.qiangqiang.task.server.entity.Comments;
import com.qiangqiang.task.server.entity.Moments;
import com.qiangqiang.task.server.mapper.CommentsMapper;
import com.qiangqiang.task.server.mapper.FollowMapper;
import com.qiangqiang.task.server.mapper.MomentsMapper;
import com.qiangqiang.task.server.mapper.provider.BaseProvider;
import com.qiangqiang.task.server.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @program: task
 * @description:
 * @author: Sky.lin
 * @create: 2019-01-05 21:02
 **/
@Service
public class MomentsService extends BaseService {

    @Autowired
    MomentsMapper momentsMapper;

    @Autowired
    FollowMapper followMapper;

    SnowflakeIdWorker momentsWorker = new SnowflakeIdWorker(0, 6);

    public boolean addMoments(Moments moments) {
        moments.setMomentsId(momentsWorker.nextId());
        moments.setCreateTime(Instant.now().toEpochMilli());
        moments.setUpdateTime(Instant.now().toEpochMilli());
        return momentsMapper.insertMoments(moments) > 0;
    }

    public List<MomentsDTO> findAllMomentsByUserId(int page, int pageSize, long userId) {
        // 找到 user 所有关注的人的Id
        Set<Long> ids=followMapper.queryFollowsOrFans(userId,false).stream()
                .map(e->e.getFollowedUserId()).collect(Collectors.toSet());
        // 查看朋友圈的时候，自己发的票圈也要显示
        ids.add(userId);
        return momentsMapper.queryAllMomentsByFollowId(page,pageSize,ids).stream()
                .map(this::beanToDTO).collect(Collectors.toList());
    }
}
